{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "WfGpInivS0fG"
   },
   "source": [
    "<h2 align=\"center\">点击下列图标在线运行HanLP</h2>\n",
    "<div align=\"center\">\n",
    "\t<a href=\"https://colab.research.google.com/github/hankcs/HanLP/blob/doc-zh/plugins/hanlp_demo/hanlp_demo/zh/dep_restful.ipynb\" target=\"_blank\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>\n",
    "\t<a href=\"https://mybinder.org/v2/gh/hankcs/HanLP/doc-zh?filepath=plugins%2Fhanlp_demo%2Fhanlp_demo%2Fzh%2Fdep_restful.ipynb\" target=\"_blank\"><img src=\"https://mybinder.org/badge_logo.svg\" alt=\"Open In Binder\"/></a>\n",
    "</div>\n",
    "\n",
    "## 安装"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "IYwV-UkNNzFp"
   },
   "source": [
    "无论是Windows、Linux还是macOS，HanLP的安装只需一句话搞定："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "id": "1Uf_u7ddMhUt"
   },
   "outputs": [],
   "source": [
    "!pip install hanlp_restful -U"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "pp-1KqEOOJ4t"
   },
   "source": [
    "## 创建客户端"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "id": "0tmKBu7sNAXX"
   },
   "outputs": [],
   "source": [
    "from hanlp_restful import HanLPClient\n",
    "HanLP = HanLPClient('https://www.hanlp.com/api', auth=None, language='zh') # auth不填则匿名，zh中文，mul多语种"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "EmZDmLn9aGxG"
   },
   "source": [
    "#### 申请秘钥\n",
    "由于服务器算力有限，匿名用户每分钟限2次调用。如果你需要更多调用次数，[建议申请免费公益API秘钥auth](https://bbs.hanlp.com/t/hanlp2-1-restful-api/53)。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "elA_UyssOut_"
   },
   "source": [
    "## 依存句法分析\n",
    "任务越少，速度越快。如指定仅执行依存句法分析："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 70
    },
    "id": "BqEmDMGGOtk3",
    "outputId": "2a0d392f-b99a-4a18-fc7f-754e2abe2e34"
   },
   "outputs": [],
   "source": [
    "doc = HanLP('2021年HanLPv2.1为生产环境带来次世代最先进的多语种NLP技术。', tasks='dep')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "返回值为一个[Document](https://hanlp.hankcs.com/docs/api/common/document.html):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\n",
      "  \"tok/fine\": [\n",
      "    [\"2021年\", \"HanLPv2.1\", \"为\", \"生产\", \"环境\", \"带来\", \"次\", \"世代\", \"最\", \"先进\", \"的\", \"多\", \"语种\", \"NLP\", \"技术\", \"。\"]\n",
      "  ],\n",
      "  \"dep\": [\n",
      "    [[6, \"tmod\"], [6, \"nsubj\"], [6, \"prep\"], [5, \"nn\"], [3, \"pobj\"], [0, \"root\"], [8, \"clf\"], [10, \"dep\"], [10, \"advmod\"], [15, \"rcmod\"], [10, \"cpm\"], [13, \"nummod\"], [15, \"nn\"], [15, \"nn\"], [6, \"dobj\"], [6, \"punct\"]]\n",
      "  ]\n",
      "}\n"
     ]
    }
   ],
   "source": [
    "print(doc)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`doc['dep']`为句子们的依存句法树列表，第`i`个二元组表示第`i`个单词的`[中心词的下标, 与中心词的依存关系]`。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "wxctCigrTKu-"
   },
   "source": [
    "可视化依存句法树："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "Zo08uquCTFSk",
    "outputId": "c6077f2d-7084-4f4b-a3bc-9aa9951704ea"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dep Tree     \tToken    \tRelati\n",
      "─────────────\t─────────\t──────\n",
      "  ┌─────────►\t2021年    \ttmod  \n",
      "  │┌────────►\tHanLPv2.1\tnsubj \n",
      "  ││┌─►┌─────\t为        \tprep  \n",
      "  │││  │  ┌─►\t生产       \tnn    \n",
      "  │││  └─►└──\t环境       \tpobj  \n",
      "┌┬┴┴┴────────\t带来       \troot  \n",
      "││        ┌─►\t次        \tclf   \n",
      "││     ┌─►└──\t世代       \tdep   \n",
      "││     │  ┌─►\t最        \tadvmod\n",
      "││  ┌─►└──┼──\t先进       \trcmod \n",
      "││  │     └─►\t的        \tcpm   \n",
      "││  │     ┌─►\t多        \tnummod\n",
      "││  │  ┌─►└──\t语种       \tnn    \n",
      "││  │  │  ┌─►\tNLP      \tnn    \n",
      "│└─►└──┴──┴──\t技术       \tdobj  \n",
      "└───────────►\t。        \tpunct \n"
     ]
    }
   ],
   "source": [
    "doc.pretty_print()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "转换为CoNLL格式："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\t2021年\t_\t_\t_\t_\t6\ttmod\t_\t_\n",
      "2\tHanLPv2.1\t_\t_\t_\t_\t6\tnsubj\t_\t_\n",
      "3\t为\t_\t_\t_\t_\t6\tprep\t_\t_\n",
      "4\t生产\t_\t_\t_\t_\t5\tnn\t_\t_\n",
      "5\t环境\t_\t_\t_\t_\t3\tpobj\t_\t_\n",
      "6\t带来\t_\t_\t_\t_\t0\troot\t_\t_\n",
      "7\t次\t_\t_\t_\t_\t8\tclf\t_\t_\n",
      "8\t世代\t_\t_\t_\t_\t10\tdep\t_\t_\n",
      "9\t最\t_\t_\t_\t_\t10\tadvmod\t_\t_\n",
      "10\t先进\t_\t_\t_\t_\t15\trcmod\t_\t_\n",
      "11\t的\t_\t_\t_\t_\t10\tcpm\t_\t_\n",
      "12\t多\t_\t_\t_\t_\t13\tnummod\t_\t_\n",
      "13\t语种\t_\t_\t_\t_\t15\tnn\t_\t_\n",
      "14\tNLP\t_\t_\t_\t_\t15\tnn\t_\t_\n",
      "15\t技术\t_\t_\t_\t_\t6\tdobj\t_\t_\n",
      "16\t。\t_\t_\t_\t_\t6\tpunct\t_\t_\n"
     ]
    }
   ],
   "source": [
    "print(doc.to_conll())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "XOsWkOqQfzlr"
   },
   "source": [
    "为已分词的句子执行依存句法分析："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 70
    },
    "id": "bLZSTbv_f3OA",
    "outputId": "111c0be9-bac6-4eee-d5bd-a972ffc34844"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dep Tree   \tToken\tRelati\n",
      "───────────\t─────\t──────\n",
      " ┌────────►\tHanLP\tnsubj \n",
      " │┌─►┌─────\t为    \tprep  \n",
      " ││  │  ┌─►\t生产   \tnn    \n",
      " ││  └─►└──\t环境   \tpobj  \n",
      "┌┼┴────────\t带来   \troot  \n",
      "││     ┌──►\t次世代  \tdep   \n",
      "││     │┌─►\t最    \tadvmod\n",
      "││  ┌─►└┼──\t先进   \trcmod \n",
      "││  │   └─►\t的    \tcpm   \n",
      "││  │  ┌──►\t多语种  \tnn    \n",
      "││  │  │┌─►\tNLP  \tnn    \n",
      "│└─►└──┴┴──\t技术   \tdobj  \n",
      "└─────────►\t。    \tpunct \n",
      "\n",
      "Dep Tree        \tTok\tRelation \n",
      "────────────────\t───\t─────────\n",
      "          ┌─►┌──\t我  \tassmod   \n",
      "          │  └─►\t的  \tassm     \n",
      "       ┌─►└─────\t希望 \ttop      \n",
      "┌┬─────┴────────\t是  \troot     \n",
      "│└─►┌───────────\t希望 \tccomp    \n",
      "│   │     ┌─►┌──\t张晚霞\tassmod   \n",
      "│   │     │  └─►\t的  \tassm     \n",
      "│   │  ┌─►└─────\t背影 \tnsubjpass\n",
      "│   └─►└──┬─────\t被  \tccomp    \n",
      "│         │  ┌─►\t晚霞 \tnsubj    \n",
      "│         └─►└──\t映红 \tdep      \n",
      "└──────────────►\t。  \tpunct    \n"
     ]
    }
   ],
   "source": [
    "HanLP(tokens=[\n",
    "    [\"HanLP\", \"为\", \"生产\", \"环境\", \"带来\", \"次世代\", \"最\", \"先进\", \"的\", \"多语种\", \"NLP\", \"技术\", \"。\"],\n",
    "    [\"我\", \"的\", \"希望\", \"是\", \"希望\", \"张晚霞\", \"的\", \"背影\", \"被\", \"晚霞\", \"映红\", \"。\"]\n",
    "  ], tasks='dep').pretty_print()"
   ]
  }
 ],
 "metadata": {
  "accelerator": "GPU",
  "colab": {
   "collapsed_sections": [],
   "name": "dep_restful.ipynb",
   "provenance": []
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
